package com.igeek.javase.list.test;
import java.util.HashMap;
import java.util.Set;

/**
 * @author chenmin
 * 2023/7/18 0:04
 * @description TODO
 * HashMap<User,String>
 * 封装 add(User元素)  添加User时，姓名和密码一致则判定是同一元素，将不予以添加
 */
public class UserHashMap {
    public static void main(String[] args) {
        HashMap<User,String> map = new HashMap<>();

        User u1 = new User("张三","342143");
        User u2 = new User("李四","536724");
        User u3 = new User("王五","965674");
        User u4 = new User("张三","342143");

        System.out.println(add(map, u1));
        System.out.println(add(map, u2));
        System.out.println(add(map, u3));
        System.out.println(add(map, u4));

        System.out.println(map);
    }

    public static boolean add(HashMap<User,String> map , User user){
        //containsKey 判断是否包含键Key，Key键重写hashCode()和equals()
        if(!map.containsKey(user)){
            map.put(user , user.getName());
            return true;
        }
        return false;
    }
}
